//
// Created by luosong on 15/8/28.
// Copyright (c) 2015 CHANJET. All rights reserved.
//

#import "CSPIdentityModel.h"
#import "CSPIdentityService.h"
#import "CSPCategory.h"

NSString *const CSPIdentityErrorDomain = @"com.chanjet.CSPIdentityErrorDomain";

@implementation CSPIdentityLoginWithAccountRequest

+ (NSDictionary *)JSONKeyPathsByPropertyKey {
    return @{
            @"account" : @"account",
            @"password" : @"pwd",
            @"coop" : @"coop",
            @"needInviteList" : @"needInviteList",
            @"endpoint_id" : @"endpoint_id",
    };
}

+ (NSValueTransformer *)JSONTransformerForKey:(NSString *)key {
    if ([key isEqualToString:@"password"]) {
        return [CSPMTLValueTransformer reversibleTransformerWithBlock:^id(id obj) {
            return [obj csp_md5String];
        }];
    }

    return nil;
}


@end

@implementation CSPIdentityGetCredentialsResponse

+ (NSDictionary *)JSONKeyPathsByPropertyKey {
    return @{
            @"account" : @"body.user.loginAccount",
            @"userId" : @"body.user.userId",
            @"name" : @"body.user.name",
            @"accessToken" : @"body.user.access_token",
            @"refreshToken" : @"body.user.refresh_token",
            @"expiration" : @"body.user.expires_in",
            @"extra" : @"body.user"
    };
}

+ (NSValueTransformer *)extraJSONTransformer {
    return [CSPMTLValueTransformer transformerWithBlock:^(NSDictionary *dictionary) {
        return [CSPMTLJSONAdapter modelOfClass:[CSPIdentityExtra class] fromJSONDictionary:dictionary error:nil];
    }];
}

@end

@implementation CSPIdentityRefreshTokenRequest
@end

@implementation CSPIdentitySignoutRequest

+ (NSDictionary *)JSONKeyPathsByPropertyKey {
    return @{
            @"accessToken" : @"access_token",
            @"clientId" : @"client_id",
            @"redirectUri" : @"redirect_uri"
    };
}

@end

#pragma mark - CSPIdentityApplySignupCodeRequest
@implementation CSPIdentityApplySignupCodeRequest

- (NSDictionary *)dictionaryValue {
    NSMutableDictionary *result = [NSMutableDictionary new];
    if (self.accountType == CSPIdentityAccountMobile) {
        return @{
                @"mobile": self.account
        };
    }
    else if (self.accountType == CSPIdentityAccountEmail) {
        return @{
                @"email": self.account
        };
    }

    return nil;
}

@end

#pragma mark - CSPIdentityApplySignupCodeResponse
@implementation CSPIdentityApplySignupCodeResponse
+ (NSDictionary *)JSONKeyPathsByPropertyKey {
    return @{
            @"errorCode": @"body.errorCode"
    };
}
@end

#pragma mark - CSPIdentitySignupRequest
@implementation CSPIdentitySignupRequest
@end